// pages/annocement/teacherANNCPage.js
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    msgHidden: true,
    alert_hidden: true,
    ab: "absolute",
    heightMsg: "",
    fixed: "",
    id:undefined,
    content:undefined,
    title:undefined,
    time:undefined,
    publisher:undefined,
    msgtitle:undefined,
    msgcontent:undefined,
    msgtime:undefined,
    msgpublisher:undefined,
    ANNCArray:[
        {
          id: 0,
          title: undefined,
          content: undefined,
          publicTime: undefined,
          publisher: undefined,
        },
    ],
  },
  inputTitle:function (e){
    console.log(e.detail.value)

       this.setData({
         title: e.detail.value
       })
    
  },
  inputContent:function (e){
    console.log(e.detail.value)
    
       this.setData({
         content: e.detail.value
       })
    
  },
    alertClick: function () {
      this.setData({
        alert_hidden: false,
        fixed: "fixed",
      })
    },
    escClick: function () {
      this.setData({
        alert_hidden: true,
        msgHidden: true
      })
    },
    msgClick: function (e) {
      var that = this;
      var touchTime = that.data.touch_end - that.data.touch_start;
      if (touchTime > 350) {  //长按删除公告
        wx.showModal({
          title: '删除公告',
          content: '确定要删除该公告？',
          showCancel: true,//是否显示取消按钮
          cancelText: "否",//默认是“取消”
          cancelColor: 'skyblue',//取消文字的颜色
          confirmText: "是",//默认是“确定”
          confirmColor: 'skyblue',//确定文字的颜色
          success: function (res) {
            if (res.cancel) {
              //点击取消,默认隐藏弹框
            } else {
              //点击确定
              that.setData({
                id: e.currentTarget.dataset.id,
              })
              wx.request({  //这一步发起网络请求 
                url: 'https://weixin0304.cn/deleteANNC.php',
                data: {
                  id: that.data.id,
                },
                header: { //'Content-Type': 'application/json'
                  'content-type': 'application/x-www-form-urlencoded'
                },
                method: 'POST',
                success: function (res) {
                  console.log(res);
                  if (res.data == 1) {
                    console.log(res.data);
                    wx.showModal({
                      title: '温馨提示',
                      content: '删除公告成功',
                      showCancel: false,
                    })
                  } else {
                    console.log(res.data);
                    wx.showModal({
                      title: '温馨提示',
                      content: '删除公告失败',
                      showCancel: false,
                    })
                  }
                  console.log('delete success');
                  that.onLoad();
                },
                fail: function (res) {
                  wx.showToast({
                    title: '您的网络开小差啦~~~',
                    icon: "none"
                  })
                },
                complete: function (res) {
                  console.log('delete complete');
                },
              })
            }
          },
          fail: function (res) { },//接口调用失败的回调函数
          complete: function (res) { },//接口调用结束的回调函数（调用成功、失败都会执行）
        })
      }
      else{//查看公告
        that.setData({
          msgHidden: false,
          msgtitle: e.currentTarget.dataset.title,
          msgcontent: e.currentTarget.dataset.content,
          msgtime: e.currentTarget.dataset.time,
          msgpublisher:e.currentTarget.dataset.publisher,
        })
      }  
    },
    mytouchstart: function (e) {
       let that = this;
       that.setData({
           touch_start: e.timeStamp  
    })
       console.log(e.timeStamp + '- touch-start')
    },
  //按下事件结束  
   mytouchend: function (e) {
     let that = this;
     that.setData({
        touch_end: e.timeStamp  
      })
       console.log(e.timeStamp + '- touch-end')
  },  
  publish: function (e) {
    var that = this
    var time = util.formatTime(new Date());
    // 再通过setData更改Page()里面的data，动态更新页面的数据
    this.setData({
      time: time
    });
   if ((undefined != this.data.content) && (undefined != this.data.title)) {
      wx.request({  //这一步发起网络请求 
        url: 'https://weixin0304.cn/teacherPublishANNC.php',
        data: {
          title: this.data.title,
          content: this.data.content,
          time:this.data.time,
          publisher:this.data.publisher,
        },
        header: { //'Content-Type': 'application/json'
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        success: function (res) {
          console.log(res);
          if (res.data == 1) {
            console.log(res.data);
            that.setData({
              alert_hidden: true
            })
            wx.showModal({
              title: '温馨提示',
              content: '发布公告成功',
              showCancel: false,
            })
            that.onLoad();
          } else {
            console.log(res.data);
            wx.showModal({
              title: '温馨提示',
              content: '发布公告失败，检查标题是否不大于20字，内容是否不大于100字',
              showCancel: false,
              success: function (res) {
              }
            })
          }
          console.log('punlish success');
        },
        fail: function (res) {
          wx.showToast({
            title: '您的网络开小差啦~~~',
            icon: "none"
          })
        },
        complete: function (res) {
          console.log('publish complete');
        },
      })
      this.setData({
        alert_hidden: true
      })
    } else {
      wx.showToast({
        title: '请将信息填写完整后提交',
        icon: "none"
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var that = this;
    var myname = wx.getStorageSync('name')
    console.log(myname)
    this.setData({
      publisher:myname
    });
    console.log(this.data.publisher)
    wx.request({
      url: 'https://weixin0304.cn/getANNCdata.php',
        method: 'GET',
      header: {
      'Content-Type': 'application/json'},
    data: {},
        success: function (res) {
          // success
          console.log(res.data);//打印请求返回的结果
          that.setData({ ANNCArray: res.data })
        },
        fail: function (res) {
          // fail
        },
        complete: function (res) {
          // complete
        }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

})
